package com.ruoyi.supermarket.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supermarket.domain.SupermarketCommodity;
import com.ruoyi.supermarket.mapper.SupermarketCommodityMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supermarket.mapper.SupermarketSaleMapper;
import com.ruoyi.supermarket.domain.SupermarketSale;
import com.ruoyi.supermarket.service.ISupermarketSaleService;

/**
 * 销售信息Service业务层处理
 * 
 * @author faye
 * @date 2024-05-27
 */
@Service
@Slf4j
public class SupermarketSaleServiceImpl implements ISupermarketSaleService 
{
    @Autowired
    private SupermarketSaleMapper supermarketSaleMapper;

    @Autowired
    private SupermarketCommodityMapper supermarketCommodityMapper;

    @Override
    public List<SupermarketSale> findAll(){
        return supermarketSaleMapper.findAll();
    }

    /**
     * 查询销售信息
     * 
     * @param saleId 销售信息主键
     * @return 销售信息
     */
    @Override
    public SupermarketSale selectSupermarketSaleBySaleId(Long saleId)
    {
        return supermarketSaleMapper.selectSupermarketSaleBySaleId(saleId);
    }

    @Override
    public SupermarketSale selectSupermarketSaleBySaleNum(Long saleNum){
        return supermarketSaleMapper.selectSupermarketSaleBySaleNum(saleNum);
    }
    /**
     * 查询销售信息列表
     * 
     * @param supermarketSale 销售信息
     * @return 销售信息
     */
    @Override
    public List<SupermarketSale> selectSupermarketSaleList(SupermarketSale supermarketSale)
    {
        return supermarketSaleMapper.selectSupermarketSaleList(supermarketSale);
    }

    /**
     * 新增销售信息
     * 
     * @param supermarketSale 销售信息
     * @return 结果
     */
    @Override
    public int insertSupermarketSale(SupermarketSale supermarketSale)
    {
        supermarketSale.setCreateTime(DateUtils.getNowDate());
        String username= SecurityUtils.getUsername();
        if(StringUtils.isBlank(username)){
            throw new RuntimeException("用户不存在");

        }
        supermarketSale.setCreateBy(username);
        changeCommodityQuantity(supermarketSale);


        return supermarketSaleMapper.insertSupermarketSale(supermarketSale);
    }



    /**
     * 修改销售信息
     * 
     * @param supermarketSale 销售信息
     * @return 结果
     */
    @Override
    public int updateSupermarketSale(SupermarketSale supermarketSale)
    {
        supermarketSale.setUpdateTime(DateUtils.getNowDate());

        String username= SecurityUtils.getUsername();
        if(StringUtils.isBlank(username)){
           // throw new RuntimeException("用户不存在");

        }
        supermarketSale.setUpdateBy(username);

        return supermarketSaleMapper.updateSupermarketSale(supermarketSale);
    }

    /**
     * 批量删除销售信息
     * 
     * @param saleIds 需要删除的销售信息主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketSaleBySaleIds(Long[] saleIds)
    {
        return supermarketSaleMapper.deleteSupermarketSaleBySaleIds(saleIds);
    }

    /**
     * 删除销售信息信息
     * 
     * @param saleId 销售信息主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketSaleBySaleId(Long saleId)
    {
        return supermarketSaleMapper.deleteSupermarketSaleBySaleId(saleId);
    }

    /**
     * 根据销售信息更改商品库存
     *
     * @param supermarketSale
     */

    public void changeCommodityQuantity(SupermarketSale supermarketSale){

        SupermarketCommodity sale_com=supermarketCommodityMapper.selectSupermarketCommodityByCommodityId(supermarketSale.getSaleCommodityId());
        if(sale_com==null){
            log.info("输入有误,商品不存在");
            supermarketSale.setRemark("输入有误,商品不存在");
            supermarketSale.setStatus("1");
            return;
            //throw new RuntimeException("商品不存在");
        }
        if((sale_com.getQuantity()-supermarketSale.getSaleQuantity())<0){
            log.info("输入有误");
            supermarketSale.setSaleQuantity(0L);
            supermarketSale.setRemark("输入有误，销售数量大于库存");
            supermarketSale.setStatus("1");
            return;
        }
        sale_com.setQuantity(sale_com.getQuantity()-supermarketSale.getSaleQuantity());
        Double uintPrice = sale_com.getUnitPrice();
        Double cost = sale_com.getCost();
//        if(cost==null) {
//            throw new RuntimeException("商品进价为空");
//        }
        supermarketSale.setSaleCost(cost);

        supermarketSale.setSalePrice(uintPrice);
        supermarketSale.setSaleProfit((uintPrice-cost)*supermarketSale.getSaleQuantity());
        supermarketSale.setRemark("输入正确");

        supermarketCommodityMapper.updateSupermarketCommodity(sale_com);
    }
}
